define(function(require, exports, module) {
    require('basic');
    require('jquerytmpl');
    require('switch');
    var config = require('config');
    var user = require('user');
    var data_str = user.info.leadPost;

    var model = function(){
        this.init();
        //console.log(config.login)
    };

    model.prototype ={
        init: function(){
            this.addEvents();
            $("[name='my-checkbox']").bootstrapSwitch();
            if($('#project_list').length){//判断加载list
                this.loadList();
            }
            if($('#project-service').length){
                this.loadProjectList();
            }
        },
        /**
         * 加载project list
         * @return {[type]} [description]
         */
        loadList: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.project_list+data_str,
                dataType: 'json',
                success: function(data){
                    for(var i=0;i<data.length;i++){
                        if(data[i]['status']==00){
                            data[i]['status_t'] = '未锁定资源';
                        }else if(data[i]['status']==01){
                            data[i]['status_t'] = '解锁资源中';
                        }else if(data[i]['status']==02){
                            data[i]['status_t'] = '锁定资源中';
                        }else if(data[i]['status']==03){
                            data[i]['status_t'] = '已锁定资源';
                        }else if(data[i]['status']==10){
                            data[i]['status_t'] = '部署成功';
                        }else if(data[i]['status']==11){
                            data[i]['status_t'] = '部署失败';
                        }else if(data[i]['status']==12){
                            data[i]['status_t'] = '部署中';
                        }else if(data[i]['status']==20){
                            data[i]['status_t'] = '已注销';
                        }else{
                            data[i]['status_t'] = '未知状态';
                        }
                    }
                    setTimeout(function(){
                        sf.loadList();
                    },30000);
                    $('#project_list tbody').html('');

                    $("#projectListTemplate").tmpl(data).appendTo("#project_list tbody");
                    var wrap = $('#project_list');
                    sf.checkAll(wrap);

                    $('.control_bar').each(function(){
                        var pop = $('#control_pop');
                        var tmp = $('#controlTemplate');
                        var list = pop.find('#control_list tbody');
                        $(this).find('button.btn').each(function(ind,itm){
                            $(itm).off('click').on('click',function(e){
                                if(wrap.find('tbody input:checked').length>0){
                                    var text = $(itm).text(),
                                        type = $(itm).data('type'),
                                        submit = $(itm).data('submit');
                                    list.html('');
                                    var data = [];
                                    wrap.find('tbody input:checked').each(function(index,el){
                                        data[index] = {};
                                        data[index]['name'] = $(el).data('name');
                                        data[index]['code'] = $(el).data('code');
                                        data[index]['projectid'] = $(el).data('projectid');
                                    });
                                    $('#submit_btn').off('click').on('click',function(){
                                        var str = '',
                                            count = 0;
                                        if(type=='DELETE'){
                                            str = data_str;
                                        }else{
                                            str = '/'+submit+data_str;
                                        };
                                        $('.loading').show();
                                        for(var i=0;i<data.length;i++){
                                            $.ajax({
                                                type: type,
                                                url: config.project+'/'+data[i]['projectid']+str,
                                                dataType: 'json',
                                                success: function(e){
                                                    count++;
                                                    if(count==data.length){
                                                        $('.loading').hide();
                                                        window.location.reload();
                                                    };
                                                },
                                                error: function(e){
                                                    count++;
                                                    if(count==data.length){
                                                        $('.loading').hide();
                                                        //window.location.reload();
                                                    };
                                                } 
                                            });
                                        };
                                    });
                                    pop.modal({keyboard: false, show: true});
                                    pop.find('.text_type').text(text);
                                    tmp.tmpl(data).appendTo(list);
                                }else{
                                    alert('请选择项目！')
                                };
                            });
                        });
                    });
                    //锁定操作
                    // $('#lockBtn').off('click').on('click', function(){
                    //     wrap.find('tbody input:checkbox').each(function(ind,el){
                    //         if($(this).prop('checked')){
                    //             var projectid = $(this).data('projectid');
                    //             $.ajax({
                    //                 type: "PUT",
                    //                 url: config.project+'/'+projectid+'/lock'+data_str,
                    //                 dataType: 'json',
                    //                 success: function(data){
                    //                     window.location.reload();
                    //                 } 
                    //             });
                    //         }
                            
                    //     });
                    // });

                    // //解锁操作
                    // $('#unlockBtn').off('click').on('click', function(){
                    //     wrap.find('tbody input:checkbox').each(function(ind,el){
                    //         if($(this).prop('checked')){
                    //             var projectid = $(this).data('projectid');
                    //             $.ajax({
                    //                 type: "PUT",
                    //                 url: config.project+'/'+projectid+'/unlock'+data_str,
                    //                 dataType: 'json',
                    //                 success: function(data){
                    //                     window.location.reload();
                    //                 } 
                    //             });
                    //         }
                            
                    //     });
                    // });

                    // //部署操作
                    // $('#deployBtn').off('click').on('click', function(){
                    //     wrap.find('tbody input:checkbox').each(function(ind,el){
                    //         if($(this).prop('checked')){
                    //             var projectid = $(this).data('projectid');
                    //             $.ajax({
                    //                 type: "PUT",
                    //                 url: config.project+'/'+projectid+'/deploy'+data_str,
                    //                 dataType: 'json',
                    //                 success: function(data){
                    //                     window.location.reload();
                    //                 } 
                    //             });
                    //         }
                            
                    //     });
                    // });

                    // //注销操作
                    // $('#cancelBtn').off('click').on('click', function(){
                    //     wrap.find('tbody input:checkbox').each(function(ind,el){
                    //         if($(this).prop('checked')){
                    //             var projectid = $(this).data('projectid');
                    //             $.ajax({
                    //                 type: "PUT",
                    //                 url: config.project+'/'+projectid+'/cancel'+data_str,
                    //                 dataType: 'json',
                    //                 success: function(data){
                    //                     window.location.reload();
                    //                 } 
                    //             });
                    //         }
                            
                    //     });
                    // });

                    // //删除操作
                    // $('#deleteBtn').off('click').on('click', function(){
                    //     wrap.find('tbody input:checkbox').each(function(ind,el){
                    //         if($(this).prop('checked')){
                    //             var projectid = $(this).data('projectid');
                    //             $.ajax({
                    //                 type: "DELETE",
                    //                 url: config.project+'/'+projectid+data_str,
                    //                 dataType: 'json',
                    //                 success: function(data){
                    //                     //window.location.reload();
                    //                 } 
                    //             });
                    //         }
                            
                    //     });
                    // });
                    
                } 
            });
        },
        /**
         * [loadProductList description]
         * @return {[type]} [description]
         */
        loadProjectList: function(){
            var sf = this;
            var project_id = $.QueryString('project_id');
            if(project_id){
                $.ajax({
                    type: "GET",
                    url: config.project+'/'+project_id+data_str,
                    dataType: 'json',
                    success: function(data){
                        data.project.dc = $.cookie('_dc');
                        var ds = data.services;
                        var dp = data.project;
                        //console.log(dp);
                        for (var i = 0; i < ds.length; i++) {
                            // if(ds[i]['service']['status']==00){
                            //     ds[i]['service']['status_t'] = '创建中';
                            // }else if(ds[i]['service']['status']==01){
                            //     ds[i]['service']['status_t'] = '创建成功';
                            // }else if(ds[i]['service']['status']==02){
                            //     ds[i]['service']['status_t'] = '创建失败';
                            // }else if(ds[i]['service']['status']==10){
                            //     ds[i]['service']['status_t'] = '启动中';
                            // }else if(ds[i]['service']['status']==11){
                            //     ds[i]['service']['status_t'] = '启动成功';
                            // }else if(ds[i]['service']['status']==12){
                            //     ds[i]['service']['status_t'] = '启动失败';
                            // }else if(ds[i]['service']['status']==20){
                            //     ds[i]['service']['status_t'] = '停用中';
                            // }else if(ds[i]['service']['status']==21){
                            //     ds[i]['service']['status_t'] = '停用成功';
                            // }else if(ds[i]['service']['status']==22){
                            //     ds[i]['service']['status_t'] = '停用失败';
                            // }else if(ds[i]['service']['status']==40){
                            //     ds[i]['service']['status_t'] = '未部署';
                            // }else{
                            //     ds[i]['service']['status_t'] = '未知状态';
                            // }
                            if(ds[i]['service']['status']==0){
                                ds[i]['service']['status_t'] = '未部署';
                                ds[i]['service']['class'] = 'off';
                            }else if(ds[i]['service']['status']==1){
                                ds[i]['service']['status_t'] = '停止';
                                ds[i]['service']['class'] = 'stop';
                            }else if(ds[i]['service']['status']==2){
                                ds[i]['service']['status_t'] = '运行';
                                ds[i]['service']['class'] = 'play';
                            }else if(ds[i]['service']['status']==3){
                                ds[i]['service']['status_t'] = '异常';
                                ds[i]['service']['class'] = 'exclamation-sign';
                            }else{
                                ds[i]['service']['status_t'] = '未知状态';
                                ds[i]['service']['class'] = 'question-sign';
                            }

                            if(ds[i]['service']['access_entrance']){
                                ds[i]['service']['access_btn'] = 'show';
                            }else{
                                ds[i]['service']['access_btn'] = 'hide';
                            }
                        }
                        // console.log(ds instanceof Array);
                        $("#baseInfoTemplate").tmpl(data).appendTo("#baseInfo");
                        $("#projectServiceTemplate").tmpl(ds).appendTo("#project-service tbody");
                        var wrap = $('#project-service');
                        sf.checkAll(wrap);

                        $('.btnacc').off('click').on('click', function(){
                            $('#alert_pop2 .username').text($(this).data('accessuser'));
                            $('#alert_pop2 .password').text($(this).data('accesspassword'));
                            $('#alert_pop2').modal({keyboard: false, show: true});
                        });

                        $('.control_bar').each(function(){
                            var pop = $('#control_pop');
                            var tmp = $('#controlTemplate');
                            var list = pop.find('#control_list tbody');
                            $(this).find('button.btn').each(function(ind,itm){
                                $(itm).off('click').on('click',function(e){
                                    if(wrap.find('tbody input:checked').length>0){
                                        var text = $(itm).text(),
                                            type = $(itm).data('type'),
                                            submit = $(itm).data('submit');
                                        list.html('');
                                        var data = [],
                                            pdata = [];
                                        wrap.find('tbody input:checked').each(function(index,el){
                                            data[index] = {};
                                            data[index]['name'] = $(el).data('name');
                                            data[index]['postid'] = $(el).data('postid');
                                            pdata.push($(el).data('postid'));
                                        });
                                        $('#submit_btn').off('click').on('click',function(){
                                            var str = '',
                                                count = 0;
                                            if(type=='DELETE'){
                                                str = data_str;
                                            }else{
                                                str = '/'+submit+data_str;
                                            };
                                            $('.loading').show();
                                            $.ajax({
                                                type: type,
                                                url: config.project+'/'+project_id+str,
                                                data: '{"services_id": '+JSON.stringify(pdata)+'}',
                                                dataType: 'json',
                                                success: function(e){
                                                    $('.loading').hide();
                                                    window.location.reload();
                                                },
                                                error: function(e){
                                                    console.log(e);
                                                    $('.loading').hide();
                                                } 
                                            });
                                        });
                                        pop.modal({keyboard: false, show: true});
                                        pop.find('.text_type').text(text);
                                        tmp.tmpl(data).appendTo(list);
                                    }else{
                                        alert('请选择项目！')
                                    };
                                });
                            });
                        });
                    } 
                });
            }
        },
        /**
         * [checkAll 全选动作]
         * @param  {[jqobj]} _wrap [description]
         * @return {[type]}       [description]
         */
        checkAll: function(_wrap){
            _wrap.find('input.checked_all').each(function(){
                $(this).removeAttr('checked');
                $(this).change(function(e) {
                    var bl = $(this).prop('checked');
                    _wrap.find('input.form_checkbox').prop({checked:bl});
                });
            });
        },


        /**
         * 添加一条新的服务标记【project-om】
         */
        addNewService: function(){
            var pop = $('#addNewService');
            pop.modal({keyboard: false, show: true,}).off('shown.bs.modal').on('shown.bs.modal',function (e) {
                
            });
        },
        
        /**
         * [addEvents 各步骤上一步下一步的事件,.done是最后提交按钮]
         */
        addEvents: function(){
            var sf = this;
            $('#addServiceBtn').off('click').on('click',sf.addNewService); //点击“服务运维”的添加按钮
        }
    };
    

    module.exports = new model();

    
});

